package com.beemans.corekit.ui.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.activity.SystemBarStyle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.viewbinding.ViewBinding;

import com.beemans.corekit.integration.language.MultiLanguages;
import com.beemans.corekit.integration.toolkit.BarUtils;

public abstract class BaseActivity<VB extends ViewBinding> extends AppCompatActivity {
    protected VB binding;

    /**
     * 子类实现此方法，提供具体的 ViewBinding 对象
     */
    @Nullable
    protected abstract VB getViewBinding();

    protected void initData(@Nullable Bundle bundle) {
    }

    /**
     * 子类实现此方法，初始化视图和设置事件
     */
    protected abstract void initViews();

    /**
     * 初始化事件
     */
    protected void initEvents() {
    }

    /**
     * 加载数据
     */
    protected void loadData() {
    }

    /**
     * 初始化观察者
     */
    protected void initObverse() {
    }

    protected void initWindow(View rootView) {
        EdgeToEdge.enable(this, SystemBarStyle.light(Color.TRANSPARENT, Color.TRANSPARENT));
        ViewCompat.setOnApplyWindowInsetsListener(rootView, (v, insets) -> {
            final Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, 0, systemBars.right, systemBars.bottom);
            return insets;
        });

        BarUtils.hideNavBar(this);
    }

    @Override
    protected void attachBaseContext(Context base) {
        try {
            super.attachBaseContext((base != null) ? MultiLanguages.attach(base) : null);
        } catch (Throwable exception) {
            super.attachBaseContext(base);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = getViewBinding();

        if (binding != null) {
            setContentView(binding.getRoot());
            initWindow(binding.getRoot());
        }
        initData(getIntent().getExtras());
        initViews();
        initEvents();
        initObverse();
        loadData();
    }
}